package com.example.administrator.baikangxing.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.adapter.BloodPressureAdapter;
import com.example.administrator.baikangxing.adapter.HeartAdapter;
import com.example.administrator.baikangxing.bean.BloodPressureBean;
import com.example.administrator.baikangxing.bean.HeartBean;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.utils.CommomUtil;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.LogUtil;
import com.example.administrator.baikangxing.utils.TimeUtils;
import com.example.administrator.baikangxing.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class PressureActivity extends BaseActivity implements View.OnClickListener {

    private TextView pressure_high;
    private TextView pressure_low;
    private ListView pressure_lv_history;
    private TextView pressure_tv_measure;
    private String select_num;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_pressure;
    }

    @Override
    public void initView() {
        base_ll_title.setBackgroundResource(R.color.heart_bg);
        setTitles("血压");
        base_ib_menu.setVisibility(View.INVISIBLE);
        pressure_high = findView(R.id.pressure_tv_high);
        pressure_low = findView(R.id.pressure_tv_low);
        pressure_lv_history = findView(R.id.pressure_lv_history);
        pressure_tv_measure = findView(R.id.pressure_tv_measure);
        pressure_tv_measure.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String low_pressure = intent.getStringExtra(Constants.LOW_PRESSURE);
        String high_pressure = intent.getStringExtra(Constants.HIGH_PRESSURE);
        pressure_high.setText(high_pressure);
        pressure_low.setText(low_pressure);
        getDataFromNet();
    }
    private void getDataFromNet() {//获取历史数据
        String url = Url.base_url+Constants.HEALTH_TARGET[1];
        select_num = OperateData.getStringData(this, Constants.SELECT_DEVICE_NUM);
        String[] values = new String[]{select_num,"hour","0","23"};
        HttpUtil.getInstance().postString(url, new String[]{"devid", "ext", "begin", "end"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    if(successString.getString("errorCode").equals("0")){
                        JSONArray message = successString.getJSONArray("message");
                        ArrayList<BloodPressureBean> list = CommomUtil.parserJsonArrayToList(message, BloodPressureBean.class);
                        BloodPressureAdapter adapter = new BloodPressureAdapter(list);
                        pressure_lv_history.setAdapter(adapter);
                    }else if(successString.getString("errorCode").equals("105")){
                        ToastUtil.showToast("暂无数据");
                    }else {
                        ToastUtil.showToast(successString.getString("errorinfo"));
                        LogUtil.e("请求失败:"+successString.getString("errorinfo"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pressure_tv_measure:
                get_pressure();
                break;
        }
    }

    private void get_pressure() {
        String[] values = new String[]{select_num,"ext"};
        HttpUtil.getInstance().postString(Url.get_pressure, new String[]{"devid", "ext"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    if(successString.getString("errorCode").equals("0")){
                        ToastUtil.showToast("已开始获取血压，请稍候");
                    }else {
                        ToastUtil.showToast("获取血压失败，请稍后重试");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }
}
